
package com.beiru.videocompress;

import android.os.Environment;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.beiru.compress.VideoCompress;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

public class RNVideocompressModule extends ReactContextBaseJavaModule {

  private final ReactApplicationContext reactContext;

  public RNVideocompressModule(ReactApplicationContext reactContext) {
    super(reactContext);
    this.reactContext = reactContext;
  }

  @Override
  public String getName() {
    return "RNVideocompress";
  }

  @ReactMethod
  public void zipVideo(String path,final Promise promise) throws IOException {
    String PACKAGE_NAME = reactContext.getPackageName();
    String compressDir = Environment.getExternalStorageDirectory().toString() + "/Android/data/" + PACKAGE_NAME + "/files/videos/";
    checkFolderExists(compressDir);
    Calendar now = new GregorianCalendar();
    SimpleDateFormat simpleDate = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
    String fileName = simpleDate.format(now.getTime());
    final String compressVideoPath = compressDir + fileName + ".mp4";
    path = path.replaceAll("file://", "");
    File file = new File(path);
    FileInputStream fis = new FileInputStream(file);
    long size = fis.available();
    if(size<= 1024*1024*10){
      //Log.d(TAG, "压缩开始压缩开始压缩开始1111111" );
      VideoCompress.compressVideoMedium(path, compressVideoPath, new VideoCompress.CompressListener() {
        @Override
        public void onStart() {
          //Log.i(TAG, "onStart: ");
        }

        @Override
        public void onSuccess() {
         // Log.i(TAG, "压缩成功" + compressVideoPath);
          promise.resolve("file://"+compressVideoPath);

        }

        @Override
        public void onFail() {

          promise.reject("-10","压缩失败");

        }

        @Override
        public void onProgress(float percent) {

        }
      });


    }else{

      VideoCompress.compressVideoLow(path, compressVideoPath, new VideoCompress.CompressListener() {
        @Override
        public void onStart() {
        }

        @Override
        public void onSuccess() {

          File file = new File(compressVideoPath);
          FileInputStream fis = null;
          try {
            fis = new FileInputStream(file);
            long size = fis.available();
            if(size >= 1024*1024*5){
              zipVideo(compressVideoPath,promise);
            }else{

              promise.resolve("file://"+compressVideoPath);
            }

          } catch (IOException e) {
            e.printStackTrace();
          }

        }

        @Override
        public void onFail() {

          promise.reject("-10","压缩失败");

        }

        @Override
        public void onProgress(float percent) {

        }
      });
    }

  }

  private void checkFolderExists(String strFolder) {
    File file = new File(strFolder);
    if (!file.exists()) {
      file.mkdir();
    }

  }


}
